﻿using Omu.ValueInjecter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SerializerBenchmark
{
    class DictionaryToEntityInjection : KnownSourceValueInjection<Dictionary<string, string>>//KnownSourceValueInjection<POCOEntity>
    {

        protected override void Inject(Dictionary<string, string> source, object target)
        {
            //if (target == null)
            //{
            //    target = new Dictionary<string, string>();
            //}
            //PropertyInfo[] props = target.GetType().GetProperties();
            //for (int i = 0; i < source.Keys.Count; i++)
            foreach (var item in source)
	        {

                string key = item.Key;
                var targetProp = target.GetProps().GetByName(key, true);
                if (targetProp == null) continue;

                //var value = pi.GetValue(source);

                //target[pi.Name] = value + string.Empty;
                switch (targetProp.PropertyType.Name)
                {
                    case "Int32":
                        targetProp.SetValue(target, Convert.ToInt32(item.Value));
                        break;
                    default:
                targetProp.SetValue(target, item.Value);
                        break;
                }
            }
        }
    }
}
